package com.example.xmpic.ui.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.common.view.dialog.SetTimeDialog;
import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivitySetAlarmPeriodBinding;
import com.example.xmpic.support.config.TimeItem;
import com.example.xmpic.ui.adapter.WeekAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2020/7/6.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置报警时段页面
 */
public class SettingAlarmPeriodActivity extends BaseBindActivity<ActivitySetAlarmPeriodBinding> {

    private int mPosition;
    private TimeItem mTimeInfo;
    private SetTimeDialog mDialog;
    private WeekAdapter mAdapter;
    private List<Bundle> mSelectedList;


    private static int[] mWeeks = new int[] {R.string.sun, R.string.mon,R.string.tue,
            R.string.wed, R.string.thu, R.string.fri, R.string.sat};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_alarm_period;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mPosition = getIntent().getIntExtra("position", 0);
        mTimeInfo = (TimeItem) getIntent().getSerializableExtra("timeInfo");
        if (null == mTimeInfo || null == mTimeInfo.getTime()){
            finish();
        }
    }

    @Override
    protected void initView() {
        super.initView();
        mSelectedList = new ArrayList<>();
        mDBind.openAlarm.setChecked(mTimeInfo.isOpen());
        mDBind.alarmDetail.setVisibility(mTimeInfo.isOpen() ? View.VISIBLE: View.GONE);
        mDBind.weekList.setLayoutManager(new GridLayoutManager(mContext, 4));
    }

    @Override
    protected void initData() {
        super.initData();
        String[] times = mTimeInfo.getTime().split("-");
        mDBind.startTime.setDetailText(times[0]);
        mDBind.endTime.setDetailText(times[1]);
        mDBind.alarmLabel.setDetailText(getString(R.string.time) + (mPosition +1));
        for (int i = 0; i < mWeeks.length; ++i) {
            Bundle bundle = new Bundle();
            bundle.putString("name", getString(mWeeks[i]));
            bundle.putBoolean("selected", ((mTimeInfo.getWeekMask() >> i) & 0x01) == 1);
            mSelectedList.add(bundle);
        }
        mAdapter = new WeekAdapter<Bundle>(mContext, mSelectedList);
        mDBind.weekList.setAdapter(mAdapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.openAlarm.setOnCheckedChangeListener((buttonView, isChecked)
                -> mDBind.alarmDetail.setVisibility(isChecked ? View.VISIBLE : View.GONE));
        mDBind.alarmSettingsBack.setOnClickListener(v -> finish());
        mDBind.startTime.setOnClickListener(v -> {
            mDialog = new SetTimeDialog(mContext, (hour, minute) -> {
                String startHour = hour<10 ? "0"+hour : hour+"";
                String startMinute = minute<10 ? "0"+minute : minute+"";
                String startTime = startHour + ":" + startMinute;
                mDBind.startTime.setDetailText(startTime);
            });
            mDialog.show();
        });
        mDBind.endTime.setOnClickListener(v -> {
            mDialog = new SetTimeDialog(mContext, (hour, minute) -> {
                String endHour = hour<10 ? "0"+hour : hour+"";
                String endMinute = minute<10 ? "0"+minute : minute+"";
                String endTime = endHour + ":" + endMinute;
                mDBind.endTime.setDetailText(endTime);
            });
            mDialog.show();
        });
        mDBind.tvSaveSet.setOnClickListener(v -> {
            if (mAdapter.getWeekMask() == 0 && mDBind.openAlarm.isChecked()){
                showToast(getString(R.string.set_weekday));
                return;
            }
            mTimeInfo.setTime(mDBind.startTime.getDetailText() + "-" + mDBind.endTime.getDetailText());
            mTimeInfo.setOpen(mDBind.openAlarm.isChecked());
            mTimeInfo.setWeekMask(mAdapter.getWeekMask());
            Intent intent = new Intent(this, CameraAlarmPeriodActivity.class);
            intent.putExtra("timeInfo", mTimeInfo);
            intent.putExtra("mPosition", mPosition);
            setResult(Activity.RESULT_OK, intent);
            finish();
        });
    }
}
